
package org.DBUtils;
 
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.ResultSet;
//import java.sql.SQLException;
import java.sql.*;
 
public class DBUtil {
 
    //这里可以设置数据库名�?
    private final static String URL = "jdbc:sqlserver://localhost;DatabaseName=db_Soft";
    private static final String USER="sa";
    private static final String PASSWORD="123456";
    
    private static Connection conn=null;
    
    //静�?�代码块（将加载驱动、连接数据库放入静�?�块中）
    static{
        try {
            //1.加载驱动程序
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功！");
            //2.获得数据库的连接
            conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("数据库连接成功！");
    }
    
    //对外提供�?个方法来获取数据库连�?
    public static Connection getConnection(){
        return conn;
    }
    
    
    public static int UpdateDB(String sql) throws SQLException
    {
    	java.sql.Statement stmt = conn.createStatement();
    	
    	int n = stmt.executeUpdate(sql);
    	
    	if(n>0)
    	{
    		System.out.println("插入成功！"+n);
    	}
    	
    	return n;
    }
    
    
	public static ResultSet SelectDB(String sql) throws SQLException{
    	java.sql.Statement stmt = conn.createStatement();
    	
    	ResultSet rs = stmt.executeQuery(sql);
    	
    	return rs;
    }
    
    
//    public static void main(String[] args) throws SQLException{
//    	String sql = "select * from JobPosted_Manage";
//    
//    	ResultSet res = SelectDB(sql);
//    	
//    	while(res.next())
//    	{
//    		System.out.println(res.getString("PostName"));
//    	}
//    	
////    	//查询语句�?
////        String sql = "select * from JobPosted_Manage";
////
////        String sql = "insert into JobPosted_Manage values('03','01','01','wuqinshan','01','01','01','2017-12-26','2019-10-15','01','01')";
////    	int n = UpdateDB(sql);
//    }
}